using UnityEngine;

public class BlackPanel : BasePanel
{
    //开启时间
    public float openTimer = 1f;
    //关闭时间
    public float closeTimer = 1f;
    //此时是上楼还是下楼
    public bool isUp = false;
    //还是此楼层吗
    public bool isOriLevel = false;
    public int currId;
    //首次进入楼层
    public bool isFirEnter = false;

    //能关闭
    public bool canClose = false;
    //赢还是输
    public int overNum = 0;//0：无，1：win,2:fail,3

    public override void OnClose()
    {
        base.OnClose();
        if(SaveManager.instance.mainData!=null)
        {
            if (SaveManager.instance.mainData.isNew==false)
            {
                //可以操作
                GameManager.Instance.canOprate = true;
            }
            else
            {
                SaveManager.instance.mainData.isNew = false;
            }
        }
        else
        {
            SaveManager.instance.mainData.isNew = false;
        }
    }
    public override void OnInit()
    {
        base.OnInit();
        path = "UI/Back/BlackPanel";
        type = PanelType.Black;
    }
    public override void OnShow(params object[] par)
    {
        base.OnShow(par);

        isUp = (bool)par[0];
        isOriLevel=(bool)par[1];
        currId=(int)par[2];
        isFirEnter=(bool)par[3];
        overNum=(int)par[4];

         GameManager.Instance.isUpStair=isUp;
        //打开open
        item.GetComponent<Animator>().SetTrigger("Open");
        //无法操作
        GameManager.Instance.canOprate = false;
    }
    private void Update()
    {
        openTimer -= Time.deltaTime;
        if(openTimer<=0&&canClose==false)
        {
            canClose = true;

            //如果是新游戏
            if (SaveManager.instance.mainData.isNew)
            {
                PanelManager.Open<NewGamePanel>();
            }

            if (overNum==1)
            {
                //关闭SayPanel,GamePanel
                PanelManager.Close("SayPanel");
                PanelManager.Close("GamePanel");
                //卸载场景
                GameManager.Instance.UnloadCurrLevel();
                //打开MainPanel
                PanelManager.Open<MainPanel>();
                return;
            }
            else if (overNum==2)
            {
                //关闭SayPanel,GamePanel
                PanelManager.Close("SayPanel");
                PanelManager.Close("GamePanel");
                //卸载场景
                GameManager.Instance.UnloadCurrLevel();
                //打开MainPanel
                PanelManager.Open<MainPanel>();
                return;
            }
            else if(overNum==3)
            {
                //关闭SayPanel,GamePanel
                PanelManager.Close("SayPanel");
                PanelManager.Close("GamePanel");
                //卸载场景
                GameManager.Instance.UnloadCurrLevel();
                //打开MainPanel
                PanelManager.Open<MainPanel>();
                return;
            }
            if (isOriLevel)
            {
                //加载此楼层
                GameManager.Instance.OriLevel(currId);
            }
            else if (isUp)
            {
                //加载
                GameManager.Instance.NextLevel(isFirEnter);
            }
            else
            {
                //加载
                GameManager.Instance.ForwardLevel();
            }
            //隐藏玩家
            GameManager.Instance.player.gameObject.SetActive(false);

            //关闭UI
            PanelManager.Close("ChooseDataPanel");
            PanelManager.Close("MainPanel");
        }

        if(canClose)
        {
            closeTimer -= Time.deltaTime;
            if(closeTimer<=0)
            {
                Close();
            }
        }
    }
}
